package com.huitone.gddw.service.impl.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.business.ISmFlowDao;
import com.huitone.gddw.dao.entity.SmFlow;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IGateDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.business.ISmFlowService;
import com.huitone.gddw.service.dto.business.SmFlowDTO;
import com.huitone.gddw.service.dto.business.SmFlowUpDTO;
import com.huitone.gddw.service.system.IRoleService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信流量统计 接口实现类
 * @author wuluodan
 *
 */
@Service
public class SmFlowService implements ISmFlowService {

	@Autowired
	private ISmFlowDao smFlowDao;
	
	@Autowired
	private IRoleService roleService;
	
	@Override
	public QueryPage<SmFlowDTO> selectPage(QueryPage<SmFlow> page) {
		List<SmFlowDTO> result = null;
		int totalCount = 0;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("userId", SysLoginUtil.getLoginUserId());
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		
		if (roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())) {
			result = smFlowDao.selectRoot(map);
			totalCount = smFlowDao.countRoot(map);
		} else {
			result = smFlowDao.selectUnit(map);
			totalCount = smFlowDao.countUnit(map);
		}
		
		QueryPage<SmFlowDTO> queryPage = new QueryPage<SmFlowDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	
	@Override
	public Map<String,Object> smsCount(QueryPage<SmFlow> page) {
		List<SmFlowDTO> result = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("serviceCode", page.getParams().get("sendType"));
		map.put("startDate", page.getParams().get("startTime"));
		map.put("endDate", page.getParams().get("endTime"));
		map.put("userId", SysLoginUtil.getLoginUserId());
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		//最多查一万条记录
		map.put("end", 10000);
		result = smFlowDao.selectRoot(map);
		int totalSend = 0;
		HashMap<String,Integer> sdayMap = new HashMap<String,Integer>();

		if(page.getParams().get("status")==null||page.getParams().get("status").equals("0")||page.getParams().get("status").equals("")){
			for (SmFlowDTO smf:result){
				if(sdayMap.get(smf.getSday())!=null){
					sdayMap.put(smf.getSday(), sdayMap.get(smf.getSday()) + smf.getRecvSuc());
				}else{
					sdayMap.put(smf.getSday(), smf.getRecvSuc());
				}
				totalSend += smf.getRecvSuc();
			}
		}else
		
//		if(page.getParams().get("status").equals("1"))
		{
			for (SmFlowDTO smf:result){
				if(sdayMap.get(smf.getSday())!=null){
					sdayMap.put(smf.getSday(), sdayMap.get(smf.getSday()) + smf.getRecvFail());
				}else{
					sdayMap.put(smf.getSday(), smf.getRecvFail());
				}
				totalSend += smf.getRecvFail();
			}
		}
		int sdayMaxSend;
		int sdayMinSend;
		
		if(result.size()!=0){
			
			
			 sdayMaxSend = sdayMap.get(result.get(0).getSday());
			 sdayMinSend = sdayMap.get(result.get(0).getSday());
			
			for(String day:sdayMap.keySet()){
				if(sdayMap.get(day)>sdayMaxSend){
					sdayMaxSend=sdayMap.get(day);
				}
				if(sdayMap.get(day)<sdayMinSend){
					sdayMinSend=sdayMap.get(day);
				}
			}
			
		}else {
			sdayMaxSend = 0;
			sdayMinSend = 0;
		}
		
		
		
		HashMap<String, Object> res = new HashMap<>();
		res.put("totalSendCount", totalSend);
		res.put("avgSendCount", result.size()==0?0:(int)(totalSend/sdayMap.size()));
		res.put("maxSendCount", sdayMaxSend);
		res.put("minSendCount", sdayMinSend);
		
		return res;
	
	}

	
	
	
	
	
	

	@Override
	public List<SmFlowDTO> getRecord(Map<String, Object> params) {
		params.put("userId", SysLoginUtil.getLoginUserId());
		if (roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())) {
			return smFlowDao.getRecordRoot(params);
		}
		return smFlowDao.getRecordUnit(params);
	}

	@Override
	public QueryPage<SmFlowUpDTO> selectPageUp(QueryPage<SmFlow> page) {
		List<SmFlowUpDTO> result = null;
		int totalCount = 0;
		
		Map<String, Object> params = page.getParams();
		params.put("isUnit", roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId()) ? "1" : "0");
		params.put("userId", SysLoginUtil.getLoginUserId());
		params.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		params.put("end", page.getPageNo() * page.getPageSize());
		
		result = smFlowDao.selectUp(params);
		totalCount = smFlowDao.countUp(params);
		
		QueryPage<SmFlowUpDTO> queryPage = new QueryPage<SmFlowUpDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public List<SmFlowUpDTO> getRecordUp(Map<String, Object> params) {
		params.put("userId", SysLoginUtil.getLoginUserId());
		params.put("isUnit", roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId()) ? "1" : "0");
		return smFlowDao.getRecordUp(params);
	}

}
